#pragma execution_character_set("utf-8")
#include "myxml.h"
#include <QFile>
#include <QDebug>

MyXML::MyXML(QObject *parent) : QObject(parent)
{

}

bool MyXML::OpenXML(QString xmlName)
{
    QString fileName = xmlName;
    this->xmlName = xmlName;
    QFile *file;
    if(!file->exists(fileName))
    {
        qDebug() << fileName << " not exist";
        return false;
    }
    else
    {
        file = new QFile(fileName);
        if(!file->open(QIODevice::ReadOnly | QFile::Text))
        {
            qDebug() << fileName << " open filed";
            return false;
        }
        QString error;
        int row = 0, column = 0;
        if(!doc.setContent(file, false, &error, &row, &column))
        {
            qDebug() << "parse file failed at line row and column" + QString::number(row, 10) + QString(",") + QString::number(column, 10);
            file->close();
            return false;
        }
        root = doc.documentElement();
        file->close();
    }
    return true;
}

QList<TestCase > MyXML::GetDetail()
{
    QDomElement domElement;
    QList<TestCase> retList;
    if( !findElement(root, domElement, "Instance") )  //查找"Instance"标签
    {
        qDebug() << "not found Instance in " << xmlName;
        return retList;
    }

    QDomNodeList list = root.childNodes();
    for(int i=0; i<list.count(); i++)
    {
        QDomElement dom;
        dom = list.at(i).toElement();
        QDomNodeList nodeList = dom.childNodes();
        TestCase testcase;
        testcase.name    = nodeList.at(0).toElement().text();
        testcase.cmd     = nodeList.at(1).toElement().text();
        testcase.counts  = nodeList.at(2).toElement().text().toInt();
        testcase.timeMax = nodeList.at(3).toElement().text().toInt();
        if(testcase.timeMax <=0 )
            testcase.timeMax = 1;
        testcase.resultStart = nodeList.at(4).toElement().text();
        testcase.resultEnd   = nodeList.at(5).toElement().text();
        testcase.resultUpper = nodeList.at(6).toElement().text().toInt();
        testcase.resultLower = nodeList.at(7).toElement().text().toInt();
        testcase.unitStr     = nodeList.at(8).toElement().text();
        if(testcase.unitStr.length() == 0)
            testcase.unitStr = "None";
        retList.append(testcase);
    }
    doc.clear();
    return retList;
}

SetupCase MyXML::GetSetupDetail()
{
    SetupCase setupcase;
    setupcase.devName  = "BOX设备";
    setupcase.testFile = "./settings/testDefault.xml";
    if(OpenXML("./settings/setup.xml")){
        QDomElement domElement;
        if( !findElement(root, domElement, "Instance") )  //查找"Instance"标签
        {
            qDebug() << "not found Instance in " << xmlName;
            return setupcase;
        }

        QDomNodeList list = root.childNodes();
        QString nodeText;
        for(int i=0; i<list.count(); i++)
        {
            QDomElement dom;
            dom = list.at(i).toElement();
            QDomNodeList nodeList = dom.childNodes();
            nodeText = nodeList.at(0).toElement().text();
            if(nodeText.length() > 0)
                setupcase.devName = nodeText;
            nodeText = nodeList.at(1).toElement().text();
            if(nodeText.length() > 0)
                setupcase.testFile     = nodeText;
        }
        doc.clear();
    }
    return setupcase;
}

bool MyXML::SetSetupDetail(const SetupCase &setupcase)
{
    if(OpenXML("./settings/setup.xml")){
        QDomElement domElement;
        if( !findElement(root, domElement, "Instance") )  //查找"Instance"标签
        {
            qDebug() << "not found Instance in " << xmlName;
            return false;
        }

        QDomNodeList list = root.childNodes();
        QDomElement dom = list.at(0).toElement();
        QDomNodeList nodeList = dom.childNodes();
        nodeList.at(0).toElement().firstChild().setNodeValue(setupcase.devName);
        nodeList.at(1).toElement().firstChild().setNodeValue(setupcase.testFile);

        QFile *file = new QFile("settings/setup.xml");
        if(!file->open(QIODevice::WriteOnly | QIODevice::Text)){
            qDebug() << "open for setup.xml error!";
            doc.clear();
        }else{
            QTextStream out(file);
            doc.save(out,4);
            file->close();
            delete file;
            doc.clear();
            return true;
        }
    }
    return false;
}

bool MyXML::findElement(QDomElement &srcElement, QDomElement &element, QString key)
{
    QDomNode node = srcElement.firstChild();

    while(!node.isNull())
    {
        if(node.isElement())
        {
            QDomElement domElement = node.toElement();
            if( domElement.tagName()==key )
            {
                element = domElement;
                return true;
            }
        }
        node = node.nextSibling();
    }
    return false;
}
